package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.List;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.data.BoardItem;
import cn.edu.zjicm.wordsnet_d.data.BoardList;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import cn.edu.zjicm.wordsnet_d.view.CircleImageView;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class SchoolRankAdapter extends BaseAdapter {

	public static final int INDEX_PUNCHOUT = 0;
	public static final int INDEX_WORD = 1;
	public static final int INDEX_SCHOOL = 2;
	private int[] imgs = new int[] { R.drawable.rank_1, R.drawable.rank_2, R.drawable.rank_3 };

	private Context context;
	private int rankIndex;
	private List<BoardItem> boardItemList;
	private boolean isMyDataOnBoard;
	private int myRank;
	private boolean isFake;

	public SchoolRankAdapter(Context context, int rankIndex, BoardList boardList, boolean isFake) {
		this.context = context;
		this.rankIndex = rankIndex;
		this.isFake = isFake;
		this.boardItemList = boardList.getBoardList();
		if (isFake && rankIndex == 0) {
			boardItemList.add(null);
		}
		this.isMyDataOnBoard = boardList.isMyDataOnBoard();
		this.myRank = boardList.getMyRank();
	}

	@Override
	public int getCount() {
		return boardItemList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return boardItemList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View arg1, ViewGroup arg2) {
		if (position == (boardItemList.size() - 1) && boardItemList.get(position) == null) {
			LinearLayout bottomLayout = new LinearLayout(context);
			AbsListView.LayoutParams ll = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 100);
			bottomLayout.setLayoutParams(ll);
			return bottomLayout;
		} else {
			View view = LayoutInflater.from(context).inflate(R.layout.view_school_rank_page_item, null);
			TextView tv1 = (TextView) view.findViewById(R.id.rank_item_tv1);
			CircleImageView iv2 = (CircleImageView) view.findViewById(R.id.rank_item_iv2);
			TextView tv3 = (TextView) view.findViewById(R.id.rank_item_tv3);
			TextView tv4 = (TextView) view.findViewById(R.id.rank_item_tv4);
			RelativeLayout rankLayout = (RelativeLayout) view.findViewById(R.id.rank_layout);

			tv1.setText(boardItemList.get(position).getRank() + "");
			String name;
			if (rankIndex == INDEX_PUNCHOUT || rankIndex == INDEX_WORD) {
				name = StringUtil.hiddenString(boardItemList.get(position).getElement());
			} else {
				name = boardItemList.get(position).getElement();
			}
			tv3.setText(name);
			tv4.setText(boardItemList.get(position).getScore() + "");

			iv2.setVisibility(View.GONE);

			if (isMyDataOnBoard) {
				if (position == 0) {
					LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.rank_item_layout);
					if (NightModeUtil.isNightModeNow()) {
						itemLayout.setBackgroundColor(context.getResources().getColor(R.color.board_first_line_night));
					} else {
						itemLayout.setBackgroundColor(context.getResources().getColor(R.color.board_first_line));
					}
					rankLayout.setBackgroundColor(context.getResources().getColor(R.color.tab_indicator_text_bg_down));
					if (NightModeUtil.isNightModeNow()) {
						tv4.setTextColor(context.getResources().getColor(R.color.word_color_night));
					} else {
						tv3.setTextColor(context.getResources().getColor(R.color.white));
						tv4.setTextColor(context.getResources().getColor(R.color.white));
					}

				} else if (position <= 3) {
					rankLayout.setBackgroundColor(context.getResources().getColor(R.color.tick_orange));
					tv1.setText("");
					tv1.setBackgroundResource(imgs[position - 1]);
					RelativeLayout.LayoutParams rl = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					int margin = Utils.changeDipToPx(context, 10);
					rl.setMargins(0, margin, 0, margin);
					tv1.setLayoutParams(rl);

				} else {
					if (NightModeUtil.isNightModeNow()) {
						rankLayout.setBackgroundColor(Color.parseColor("#53555d"));
					} else {
						rankLayout.setBackgroundColor(context.getResources().getColor(R.color.board_gray));
					}
				}

				if (position > 4 && boardItemList.get(position).getRank() == myRank) {
					rankLayout.setBackgroundColor(context.getResources().getColor(R.color.tab_indicator_text_bg_down));
				}

			} else {
				if (position <= 2) {
					rankLayout.setBackgroundColor(context.getResources().getColor(R.color.tick_orange));
					tv1.setText("");
					tv1.setBackgroundResource(imgs[position]);
					RelativeLayout.LayoutParams rl = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					int margin = Utils.changeDipToPx(context, 10);
					rl.setMargins(0, margin, 0, margin);
					tv1.setLayoutParams(rl);

				} else {
					if (NightModeUtil.isNightModeNow()) {
						rankLayout.setBackgroundColor(Color.parseColor("#53555d"));
					} else {
						rankLayout.setBackgroundColor(context.getResources().getColor(R.color.board_gray));
					}
				}
			}

			LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.rank_item_linearLayout);
			switch (rankIndex) {
			case INDEX_PUNCHOUT:
				linearLayout.setVisibility(View.GONE);
			case INDEX_WORD:
				linearLayout.setVisibility(View.GONE);
			case INDEX_SCHOOL:
				linearLayout.setVisibility(View.GONE);
			}

			return view;
		}
	}

	public void updateListData(BoardList boardListParent) {
		this.boardItemList = boardListParent.getBoardList();
		if (boardItemList.size() > 0) {
			boardItemList.add(null);
		}
		this.isMyDataOnBoard = boardListParent.isMyDataOnBoard();
		this.myRank = boardListParent.getMyRank();
		notifyDataSetChanged();
	}

}
